"use server";
import { listQuestionBankVoByPageUsingPost } from "@/api/questionBankController";
import { listQuestionVoByPageUsingPost } from "@/api/questionController";
import QuestionBankList from "@/components/QuestionBankList";
import { Divider, Flex } from "antd";
import Title from "antd/es/typography/Title";
import Link from "next/link";
import "./index.css";
import QuestionList from "@/components/QuestionList";
/**
 * 主页
 * @returns
 */
export default async function HomePage() {
  let bankPageParams = {
    current: 1,
    pageSize: 12,
    sortField: "createTime",
    sortOrder: "descend",
  };
  let bankList: API.QuestionBankVO[] = [];
  try {
    await listQuestionBankVoByPageUsingPost(bankPageParams).then((res) => {
      const { data } = res;
      bankList = (data as API.PageQuestionVO_)?.records ?? [];
    });
  } catch (error: any) {
    console.error("获取题库失败" + error.message);
  }

  let questionList: API.QuestionVO[] = [];
  let questionPageParams = {
    current: 1,
    pageSize: 12,
    sortField: "createTime",
    sortOrder: "descend",
  };
  try {
    await listQuestionVoByPageUsingPost(questionPageParams).then((res) => {
      const { data } = res;
      questionList = (data as API.PageQuestionVO_)?.records ?? [];
    });
  } catch (error: any) {
    console.error("获取题目失败" + error.message);
  }

  return (
    <div id="homePage" className="max-width-content">
      <Flex align="center" justify="space-between">
        <Title level={2}>最新题库</Title>
        <Link href="/banks">查看更多</Link>
      </Flex>
      <div>
        <QuestionBankList questionBankList={bankList} />
      </div>
      <Divider />
      <Flex align="center" justify="space-between">
        <Title level={2}>最新题目</Title>
        <Link href="/questions">查看更多</Link>
      </Flex>
      <QuestionList questionList={questionList} />
    </div>
  );
}
